package br.com.cnpv.managebean.combustivel;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

import br.com.cnpv.modelo.bo.CombustivelBO;
import br.com.cnpv.modelo.entity.Combustivel;

@ManagedBean(name="combustivelMB")
@SessionScoped
public class CombustivelMB extends CombustivelBean{

	public void buscar()
	{
		try
		{
			// BO
			CombustivelBO bo = CombustivelBO.getInstance();

			combustivelList = bo.buscar(combustivel);
		}catch(Exception e)
		{
			FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "", "Ocorreu um erro. Tente mais tarde");
			FacesContext.getCurrentInstance().addMessage(null, message);
			e.printStackTrace();
		}
	}
	
	public String paginaNovo()
	{
		combustivel = new Combustivel();
		return "pretty:novoCombustivel";
	}
	
	public void saveOrUpdate()
	{
		FacesMessage message = null;
		try
		{
			//BO
			CombustivelBO bo = CombustivelBO.getInstance();
			bo.saveOrUpdate(combustivel);
			if(combustivel.getId() == 0)
			{
				message = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Combustivel Cadastrado com Sucesso!");
				combustivel = new Combustivel();
			}else
			{
				message = new FacesMessage(FacesMessage.SEVERITY_INFO, "", "Combustivel Alterado com Sucesso!");
			}
		}catch(Exception e)
		{
			message = new FacesMessage(FacesMessage.SEVERITY_FATAL, "", "Ocorreu um erro. Tente mais tarde");
			e.printStackTrace();
		}
		
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	
	public String paginaAlterar(SelectEvent event)
	{
		try
		{
			combustivel = combSelecionado;
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return "pretty:alterarCombustivel";
	}
}